
var networkTool = require("../../utils/networkTool.js");
var cacheKeys = require("../../utils/keys.js");
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchValue: null
  },

  searchValue: function (e) {
    this.data.searchValue = e.detail.value;
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var data = wx.getStorageSync(cacheKeys.headcachekey());
    if (data) {
      this.setMainData(data);
    } else {
      networkTool.ahome(this.mainData);
    }
  },

  mainData: function (rep) {
    var data = rep.VIPCONTENT;
    this.setMainData(data);
    wx.setStorageSync(cacheKeys.headcachekey(), data)
  },

  setMainData: function (data) {
    var headImg = [];
    var contentImg = [];
    for (var d in data) {
      if (data[d].NO_IMGTYPE == 1) {
        contentImg.push(data[d]);
      } else {
        headImg.push(data[d]);
      }
    }
    this.setData({
      headImg: headImg,
      contentImg: contentImg
    });
  },

  onSwiperItemTap: function (event) {
    var weburl = event.currentTarget.dataset.weburl;
    wx.navigateTo({
      url: '/pages/webpage/webpage?url=' + weburl
    })
  },

  onContentTap: function (event) {
    var code = event.currentTarget.dataset.go;
    this.searchData(code, "");
  },

  //扫描
  onScanImgTap: function (event) {
    // 允许从相机和相册扫码
    wx.scanCode({
      success: (res) => {

      }
    });
  },

  showDataTap: function (event) {
    this.searchData("", "");
  },

  //回车确认
  onBindConfirm: function (event) { 
    var value = event.detail.value;
    if (value && value.length > 0) {
      this.searchData("", value);
    }
  },

  onSearchConfirm: function (event) {
    var value = this.data.searchValue;
    if (value && value.length > 0) {
      this.searchData("", value);
    }
  },
  /**
  * 查询数据
  */
  searchData: function (label, value) {
    app.globalData.searchInfo = {
      ST_SCONDITION: value,
      ST_LABEL: label,
      ST_FILTERFIELD: "",
      ST_SEQUENCE: "",
      NO_PAGE: 1.0
    };
    wx.navigateTo({
      url: '/pages/item/item'
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})